import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      '/api/bfl': {
        target: 'https://api.bfl.ai',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/bfl/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('📤 BFL代理请求:', req.url, '→', proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('� BFL代理响应:', req.url, '状态:', proxyRes.statusCode)
          })
        }
      },
      '/api/bfl-eu': {
        target: 'https://api.eu.bfl.ai',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/bfl-eu/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('📤 BFL-EU代理请求:', req.url, '→', proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📨 BFL-EU代理响应:', req.url, '状态:', proxyRes.statusCode)
          })
        }
      },
      '/api/bfl-us': {
        target: 'https://api.us.bfl.ai',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/bfl-us/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('📤 BFL-US代理请求:', req.url, '→', proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📨 BFL-US代理响应:', req.url, '状态:', proxyRes.statusCode)
          })
        }
      },
      // 添加对旧版本端点的支持
      '/api/bfl-us1': {
        target: 'https://api.us1.bfl.ai',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/bfl-us1/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('📤 BFL-US1代理请求:', req.url, '→', proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📨 BFL-US1代理响应:', req.url, '状态:', proxyRes.statusCode)
          })
        }
      },
      '/api/bfl-eu1': {
        target: 'https://api.eu1.bfl.ai',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/bfl-eu1/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('📤 BFL-EU1代理请求:', req.url, '→', proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📨 BFL-EU1代理响应:', req.url, '状态:', proxyRes.statusCode)
          })
        }
      }
    }
  }
})
